﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace iLapKan
{
    class DateTimeLP
    {
        #region DateTime Process
        public static DateTime NullDateTime { get { return new DateTime(1900, 1, 1, 0, 0, 0).Date; } }
        public static DateTime MaxDateTime { get { return new DateTime(9998, 1, 1, 0, 0, 0).Date; } }
        private static TimeSpan ShiftTime;

        public static String LKError;

        //public static DateTime GetServerDateTime()
        //{
        //    if (ShiftTime.Ticks == 0)
        //    {
        //        ShiftTime = DBManager.GetServerDateTime().Subtract(DateTime.Now);
        //    }

        //    return DateTime.Now.Add(ShiftTime);
        //}

        public static DateTime LayThoiGianTuChuoi(String dateString)
        {
            int ngay = 1;
            int thang = 1;
            int nam = 1900;
            String[] dateArray = StringLP.SplitByAny(dateString, "/");
            if (NumberLP.getInt(dateArray[0]) <= 31)
            {
                ngay = NumberLP.getInt(dateArray[0]);
            }

            if (NumberLP.getInt(dateArray[1]) <= 12)
            {
                thang = NumberLP.getInt(dateArray[1]);
            }

            if (NumberLP.getInt(dateArray[2]) < 2500)
            {
                nam = NumberLP.getInt(dateArray[2]);
            }
            return new DateTime(nam, thang, ngay);
        }

        public static DateTime LayDateTimeFromString(string val)
        {
            if (val.IndexOf("/") == val.LastIndexOf("/")) return NullDateTime;
                //GetServerDateTime();
            string[] str = val.Split('/');
            int mon = Convert.ToInt16(str[0]);
            int day = Convert.ToInt16(str[1]);
            int yea = Convert.ToInt16(str[2]);
            return new DateTime(yea, mon, day);
        }

        public static DateTime LayDateOnly(DateTimePicker dtpcontrol)
        {
            DateTime ngayhientai = dtpcontrol.Value;

            int mon = ngayhientai.Month;
            int day = ngayhientai.Day;
            int yea = ngayhientai.Year;
            return new DateTime(yea, mon, day);
        }

        public static DateTime LayDateStandard(Object obj)
        {
            try
            {
                if (obj == null)
                    return NullDateTime;
                return (DateTime)obj;
            }
            catch
            {
                return NullDateTime;
            }
        }
        #endregion
    }
}
